#include <iostream>
#include <set>
#include <map>
#include <string>
#include <utility>
using std::cout;
using std::endl;
using std::pair;
using std::string;
using std::set;
using std::map;
using std::make_pair;

void test0(){
    string str1("hello");
    int num = 1;
    pair<int,string> p1(num,str1);
    pair<int,string> p2(10,"world");

    cout << p1.first << "---->" << p1.second << endl;
    cout << p2.first << "---->" << p2.second << endl;
}

//set的插入操作
void test1(){   
    cout << "set插入单个元素" << endl;
    set<int> nums = {1,2,3,4,5};
    pair<set<int>::iterator,bool> ret = nums.insert(3);
    /* auto ret = nums.insert(3); */
    if(ret.second){
        cout << "插入成功！" << *ret.first << endl;
    }else{
        cout << "插入失败！该元素本已存在!" << *ret.first << endl;
    }

    cout << endl;
    cout << "set插入多个元素" << endl;
    set<int> nums2{2,4,8,9,2,1,10};
    nums.insert(nums2.begin(),--nums2.end());

    for(auto & ele : nums){
        cout << ele << " ";
    }
    cout << endl;

    int arr[5] = {23,78,2,7,11};
    nums.insert(arr,arr + 5);
    for(auto & ele : nums){
        cout << ele << " ";
    }
    cout << endl;

    nums.insert({34,89,1,3});
    for(auto & ele : nums){
        cout << ele << " ";
    }
    cout << endl;

    auto it = nums.begin();
    ++it;
    cout << *it << endl;
    /* *it = 100; */

}

//map的构造和基本操作
void test2(){
    pair<int,string> p2(10,"world");
    map<int,string> nums = {
        {1,"hello"},
        {2,"world"},
        {3,"wangdao"},
        p2,
        pair<int,string>(4,"beijing"),
        pair<int,string>(6,"wangdao"),
        make_pair(9,"shenzhen"),
        make_pair(10,"guangzhou"),
        make_pair(1,"hangzhou")
    };

    //1.map根据pair的key值去重
    //2.map根据pair的key值默认升序排列
    for(auto & pa : nums){
        cout << pa.first << " " << pa.second << endl;
    }

    cout << endl;
    map<int,string>::iterator it = nums.begin();
    for(; it != nums.end(); ++it){
        /* cout << (*it).first << " " << (*it).second << endl; */
        cout << it->first << " " << it->second << endl;
    }
}

//map查找/插入操作
void test3(){  
    map<int,string> nums = {
        {1,"hello"},
        {2,"world"},
        {3,"wangdao"},
        pair<int,string>(4,"beijing"),
        pair<int,string>(6,"wangdao"),
        make_pair(9,"shenzhen"),
        make_pair(10,"guangzhou"),
        make_pair(1,"hangzhou")
    };

    cout << nums.count(11) << endl;
    auto it = nums.find(19);
    if(it != nums.end()){
        cout << "该元素存在" << endl;
        cout << it->first << " " << it->second << endl;
    }else{
        cout << "该元素不存在" << endl;
    }

    cout << endl;
    cout << "map插入单个元素" << endl;
    /* auto ret = nums.insert(pair<int,string>(7,"dalian")); */
    pair<map<int,string>::iterator,bool> ret = 
        nums.insert(pair<int,string>(6,"dalian"));

    if(ret.second){
        cout << "插入成功" << endl;
        cout << ret.first->first << " " << ret.first->second << endl; 
    }else{
        cout << "插入失败，该元素已存在" << endl;
        cout << ret.first->first << " " << ret.first->second << endl; 
    }

    cout << endl;
    cout << "map插入多个元素" << endl;
    nums.insert({{1,"shenyang"},{40,"changchun"},{7,"chengdu"}});
    for(auto & pa : nums){
        cout << pa.first << " " << pa.second << endl;
    }
}

//map的下标操作
void test4(){
    map<int,string> nums = {
        {1,"hello"},
        {2,"world"},
        {3,"wangdao"},
        pair<int,string>(4,"beijing"),
        pair<int,string>(6,"wangdao"),
        make_pair(9,"shenzhen"),
        make_pair(10,"guangzhou"),
        make_pair(1,"hangzhou")
    };

    cout << nums[10] << endl;
    cout << nums[20] << endl;
    for(auto & pa : nums){
        cout << pa.first << " " << pa.second << endl;
    }

    cout << endl;
    nums[2] = "wangdao";
    nums[20] = "wangdao";
    for(auto & pa : nums){
        cout << pa.first << " " << pa.second << endl;
    }

}

void test5(){
    map<string,int> nums = {
        {"wangdao",1},
        {"hello",2},
        {"test",3}
    };

    for(auto & pa : nums){
        cout << pa.first << " " << pa.second << endl;
    }

    cout << nums["world"] << endl;
    for(auto & pa : nums){
        cout << pa.first << " " << pa.second << endl;
    }

}

int main(void){
    test5();
    return 0;
}
